async function teachergetall(){
    response=await taskObject.fetchtemplate(path='/api/admin/teacher')
    return response
}
async function openTeacherForm() {
    layui.use('layer', function () {
        var layer = layui.layer;

        // 构建弹窗内容
        var formContent = `
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">教师姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="teacherName" required lay-verify="required" placeholder="请输入教师姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">年龄</label>
                    <div class="layui-input-block">
                        <input type="text" name="age" required lay-verify="required|number" placeholder="请输入年龄" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <select name="gender" lay-verify="required">
                            <option value="男">男</option>
                            <option value="女">女</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">手机号</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" required lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="submitForm">提交</button>
                    </div>
                </div>
            </form>
        `;

        // 弹出窗口
        layer.open({
            type: 1,
            title: '教师信息录入',
            area: ['400px', '400px'],
            content: formContent,
            success: function (index,a) {
                
                layui.form.render();

              
                layui.form.on('submit(submitForm)',async function (data) {
                 
                     response=await taskObject.fetchtemplate(path='/api/admin/teacher',method='POST',data=data.field)
                    setTimeout(() => {
                     if(response.code==200){
                        layer.msg('添加老师成功',{icon:1,time:2000})
                        setTimeout(()=>{
                          window.location.reload();

                        },1500)
                        layer.close(a)
                        return false;

                     }

                     layer.msg(response['message'],{icon:2,time:1500})
                
                    }, 1500);
                    layer.close(a)
                     return false; // 阻止表单提交
                });
               
            }
        });
        $('.layui-form').on('submit', async function(even){
            even.preventDefault();
        })
    });
}
!(async function(){
    response=await teachergetall()
    dataform['teacherdata']=response.data
   
    await rendertable(data=response['data'],domhtml='.teacher tbody',domdata=['id','username','age','gender','phone','password'])
   
    for (const i in response['data']){
    
        $(`.teacher tbody tr:eq(${i})`).append(`<td><button class="layui-btn layui-bg-red " id=${response['data'][i]['id']} onclick=deleteteacher(this)>删除</button><button class="layui-btn layui-bg-blue" onclick="openEditor(this)" id=${response['data'][i]['id']}>编辑</button></td>`)
    }
    $('.teacher i').on('click',async ()=>{
       await openTeacherForm()
    })
}())
async function deleteteacher(even){
    
    layer.confirm('确认删除?', {
        btn: ['确定', '关闭'] //按钮
      }, async function(){
        let valueId=even.id;
        valueId=parseInt(valueId)
       
        response=await taskObject.fetchtemplate(path='/api/admin/teacher',method='DELETE',data={'userid':valueId})
        setTimeout(()=>{
            if(response['code']==200){
                layer.msg('删除成功',{icon:1,time:1500})
                setTimeout(()=>{
                    window.location.reload();
                },1000)
                return ;
            }
            layer.msg('删除失败',{icon:2,time:1500})
            
        },1500) 
    }, function(){
       
      });
   }



 async  function openEditor(even) {
    let valueId=even.id;
    valueId=parseInt(valueId)
    console.log(valueId);
   let  updict=null
    for (key of dataform.teacherdata){
        if (key.id==valueId){
         updict=key
        }
    }
    
    console.log(updict);
    let uphtml=`  <form action="" class="layui-form">
    <div class="layui-form-item">
        <label for="" class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value=${updict.phone} id="phone">
        </div>
    </div>
    <div class="layui-form-item">
        <label for="" class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value=${updict.password} id="password">
        </div>
    </div>
    <div class="layui-form-item">
        <label for="" class="layui-form-label">年龄</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value=${updict.age} id="age">
        </div>
    </div>
    <div class="layui-form-item">
     
        <label for="" class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <select name="" id="gender">
                <option value="true">男</option>
                <option value="false">女</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
    <div class="layui-input-block">
    
    <button class="layui-btn layui-bg-blue" lay-submit lay-filter="saveForm">确认</button>
 
</div>
           </div>
</form>`
    layer.open({
        title: '编辑弹出框',
        content: uphtml,
        area: ['300px', '300px'],
        btn:[],
        success:async (data,index)=>{
            layui.use('form',async ()=>{
                let form=layui.form;
                form.render();


                form.on('submit(saveForm)', async function(data){
                    let phone=$('#phone').val();
                let password=$('#password').val();
                let gender=$('#gender').val();
                let age=$('#age').val();
                if (phone==''||password==''||gender==''||age==''){
                    return layer.msg('请填写完整信息');
                }
                let data1={'phone':phone,'password':password,'gender':gender,'age':age,'id':valueId}
                response=await taskObject.fetchtemplate(path='/api/admin/teacher',method='PUT',data=data1)
                setTimeout(()=>{
                  if(response.code==200){
                    layer.msg('更新成功',{icon:1,time:1500});
                    setTimeout(()=>{
                        window.location.reload();
                    },2000)
                    layer.close(index);
                    return false;
                  }
                  layer.msg(response['messge'],{icon:2,time:1500});
                  layer.close(index);
                  return false;
                },1000)
                  
                    return false;
                });

            })
          $('form').on('submit', function(even){even.preventDefault();})
           

            
        }
    });
}


